// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      { id: 1, title: "账号", placeholder: "请输入账号", password: false, submitNames: 'account', type:'text'},
      { id: 2, title: "密码", placeholder: "请输入密码", password: true, submitNames: 'password', type: 'text' },
      { id: 3, title: "确认密码", placeholder: "请输入密码", password: true, submitNames: 'passwordConfirm', type: 'text'},
      { id: 4, title: "姓名", placeholder: "请输入姓名", password: false, submitNames: 'name', type: 'text' },
      { title: "性别", placeholder: "请输入性别", password: false, submitNames: 'sex'},
      { id: 5, title: "手机号", placeholder: "请输入手机号", password: false, submitNames: 'phoneNumber', type: 'number' }
    ],
    key: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ,
    loginPage: "/pages/login/login",

    password: "",
    passwordConfirm: "",
    userNameFlag:false,
    passwordFlag: false,
    phoneFlag:false
  },

  inutLostFocus: function (e) {
    var curId = e.currentTarget.id;
    var value = e.detail.value;
    // console.log("LostFocus:" + curId + " value:" + value);
    this.checkInput(curId, value);
  },

  //检查输入是否满足要求
  checkInput: function (currentId, value) {
    console.log("Check:" + currentId + " value:" + value);
    if (currentId == 1) {
      this.checkIfUserExist(value);
    }
    else if (currentId == 2) {
      //密码
      this.password = value;
      if (this.passwordConfirm != undefined) {
        if (this.passwordConfirm != this.password) {
          console.log("Password Not Match");
          this.passwordFlag = false;
          this.showWarning('两次密码输入不匹配');
        }
        else {
          console.log("Password  Match!");
          this.passwordFlag = true;
        }
      }
    }
    else if (currentId == 3) {
      //确认密码
      this.passwordConfirm = value;
      if (this.password != undefined) {
        if (this.passwordConfirm != this.password) {
          console.log("Password Not Match");
          this.passwordFlag = false;
          // this.showWarning('两次密码输入不匹配');
          this.showWarning('两次密码输入不匹配' );
        }
        else {
          console.log("Password  Match!");
          this.passwordFlag = true;
        }
      }

    }
    else if (currentId == 5) {
      //手机号
      this.phoneFlag = this.checkPhoneNumber(value);
    }

  },

  checkIfUserExist:function(userName){
    var self = this;
    var prefix = getApp().globalData.serverUrlPrefix ;
    wx.request({
      url: prefix+'/CarRental//User/ExistCheck', //Register
      data: {
        userName: userName,
      },

      header: {
        'content-type': 'application/json;' // 默认值
      },
      success: function (res) {
        // self.showWarning(res.data);
        console.log("Success:  From Server:" + res.data)

        if(res.data == true){
          self.showWarning("用户名已被占用,请重新输入");
          self.data.userNameFlag = false;
        }else{
          self.data.userNameFlag = true;
        }
      },
      fail: function (res) {
        // console.log("网络错误,请检查您的网络连接");
        self.showWarning('网络错误,无法查询账号是否被占用');
      },

    })
  },


  checkPhoneNumber: function (mobile) {
    if (mobile.length != 11) {
      this.showWarning('手机号长度错误');
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      this.showWarning('手机号格式错误');
      return false;
    }
    return true;
  },

  //Toast
  showWarning: function (text) {
    wx: wx.showToast({
      title: text,
      icon: 'none',
      image: '',
      duration: 2000,
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //Form
  //注册按钮点击事件
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);

    var value = e.detail.value;
    //检查数据合法性 
    var legal = true;

    //Check User Exist
    if (this.data.userNameFlag != true){
      legal = false;
    }

    //check password
    if (this.passwordFlag != true) {
      legal = false;
    }
    //check cellphone
    if (this.phoneFlag != true) {
      legal = false;
    }
    //Empty Check
    if (value.account == undefined || value.account == "" )
    {
      legal = false;
    }
    if (value.name == undefined || value.name == "") {
      legal = false;
    }
    if (value.sex == undefined || value.sex == "") {
      legal = false;
    }

    //Test 
    // legal = true;

    if (legal == false) {
      console.log("表单数据不合法");
      //Improve
      this.showWarning('表单数据不合法');
      return false;
    }

    this.requestRegister(e.detail.value);
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },

  //Network
  requestRegister: function (value) {

    // //Fix   
    // // Chinese Encode Problem
    // if (value.sex == "男") {
    //   value.sex = "m";
    // }
    // else if (value.sex == "女") {
    //   value.sex = "f";
    // }
    var self = this;
    wx.request({
      url: 'http://localhost:8090/CarRental/User/Register', //Register
      data: {
        account: value.account,
        password: value.password,
        passwordConfirm: value.passwordConfirm,
        name: value.name,
        sex: value.sex,
        phoneNumber: value.phoneNumber
      },

      header: {
        'content-type': 'application/json;' // 默认值
      },
      success: function (res) {
        self.showWarning(res.data);
        // console.log("Success:  From Server:" + res.data)
      },
      fail: function (res) {
        console.log("网络错误,请检查您的网络连接");
      },

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})